import request from '../../utils/request';

// pages/personal/personal.js
let startY = 0; // 起始坐标
let moveY = 0; // 实时坐标
let moveDistance = 0; // 移动的距离

Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		coverTransform: 'translateY(0)',
		coverTransition: '',
		userInfo: {},
		recentList: [],
	},
	onLoad: function (options) {},
	onShow: async function () {
		if (this.data.userInfo.nickname) {
			return;
		}
		let userInfo = wx.getStorageSync('userInfo');
		// 没有登录的时候，本地存储数据是为空的
		if (userInfo) {
			this.setData({ userInfo });
			let recentListData = await request('/user/record', { uid: userInfo.userId, type: 1 });
			let index = 0;
			let recentList = recentListData.weekData.map((item) => {
				item.id = index++;
				return item;
			});
			this.setData({ recentList });
		}
	},
	handleTouchStart(e) {
		startY = e.touches[0].clientY;
		this.setData({
			coverTransition: '',
		});
	},
	handleTouchMove(e) {
		moveY = e.touches[0].clientY;
		// 计算手指纵向实时移动的距离
		moveDistance = moveY - startY;

		if (moveDistance < 0) {
			return;
		}
		if (moveDistance > 80) {
			moveDistance = 80;
		}
		// 更新 coverTransform 状态值
		this.setData({
			coverTransform: `translateY(${moveDistance}rpx)`,
		});
	},
	handleTouchEnd(e) {
		this.setData({
			coverTransform: `translateY(0)`,
			coverTransition: 'transform 1s linear',
		});
	},
	toLogin() {
		if (this.data.userInfo.nickname) {
			return;
		}
		wx.navigateTo({
			url: '/pages/login/login',
		});
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {},

	/**
	 * 生命周期函数--监听页面显示
	 */

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {},
});
